import storage from "./storage";
import axios from './axios'
import token from './token'
import { Menu } from "@/types";
import moment from 'moment'

const ListToTree = (list: Menu[], tree: Menu[], pid: number = 0) => {
    list.forEach(item => {
        // 判断是否为父级菜单
        if (item.parent_id == pid) {
            const child: Menu[] = []
            // 迭代 list， 找到当前菜单相符合的所有子菜单
            ListToTree(list, child, item.id)
            // 删掉不存在 children 值的属性
            if (child.length > 0) {
                // 加入到树中
                tree.push({ ...item, children: child })
            } else {
                tree.push({ ...item })
            }
        }
    })
}

const datefmt = (input: number, fmtstring: string) => moment.unix(input).format(fmtstring)


export {
    storage,
    axios,
    token,
    ListToTree,
    datefmt
}
